package com.sskj.mine.ui.activity;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.PwdForgetActivityPresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.MINE_PWD_FORGET)//忘记密码
public class PwdForgetActivity extends BaseActivity<PwdForgetActivityPresenter> {
    @BindView(R2.id.etPhone)
    EditText etPhone;
    @BindView(R2.id.btNextStep)
    TextView btNextStep;
    @BindView(R2.id.tvToLogin)
    TextView tvToLogin;
    @BindView(R2.id.tvCode)
    TextView tvCode;
    @BindView(R2.id.etCode)
    EditText etCode;
    DisposableSubscriber disposableSubscriber;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_pwd_forget;
    }

    @Override
    public PwdForgetActivityPresenter getPresenter() {

        return new PwdForgetActivityPresenter();
    }

    @Override
    protected void initView() {
        ClickUtil.click(tvCode, () -> {
            if (CheckUtil.isMobile(etPhone.getText().toString())) {
                mPresenter.sendCode(etPhone.getText().toString());
            } else if (CheckUtil.isEmail(etPhone.getText().toString())) {
                mPresenter.sendEmailCode(etPhone.getText().toString());
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.minePwdForgetActivity0));
            }
        });
        ClickUtil.click(btNextStep, () -> {
            if (!TextUtils.isEmpty(etCode.getText())) {
                mPresenter.checkCode(etPhone.getText().toString(), etCode.getText().toString());
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity2));
            }
        });
        ClickUtil.click(tvToLogin, () -> {
            ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();
        });
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    btNextStep.setEnabled(true);
                } else {
                    btNextStep.setEnabled(false);
                }
            }
        });
    }

    public void sendCodeOk() {
        startCount();
    }

    public void sendCodeError() {
        tvCode.setEnabled(true);
    }

    public void startCount() {
        tvCode.setEnabled(false);
        DisposUtil.close(disposableSubscriber);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvCode.setText(time + App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity3));
//                tvCode.setTextColor(getResources().getColor(R.color.mine_adc));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvCode.setText(App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity4));
                tvCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposableSubscriber != null && !disposableSubscriber.isDisposed()) {
            disposableSubscriber.dispose();
            disposableSubscriber = null;
        }
    }

    public void setPwd(String account) {
        tvCode.setText(App.INSTANCE.getString(R.string.mineCodeCheckDialogActivity4));
        tvCode.setEnabled(true);
        if (disposableSubscriber != null&& !disposableSubscriber.isDisposed()){
            disposableSubscriber.dispose();
            disposableSubscriber = null;
        }
        ARouter.getInstance().build(RConfig.MINE_PWD_RESET)
                .withString(Constans.PHONE, account)
                .withString(Constans.CODE, etCode.getText().toString())
                .navigation();
    }


}
